package com.purchasing.system.repository;

import com.purchasing.system.model.ERole;
import com.purchasing.system.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface RoleRepository extends JpaRepository<Role, Long> {
    Optional<Role> findByName(ERole name);
    boolean existsByName(ERole name);

    // 新增方法：检查权限是否被分配给任何角色
    @Query("SELECT COUNT(r) > 0 FROM Role r JOIN r.permissions p WHERE p.id = :permissionId")
    boolean isPermissionAssignedToAnyRole(@Param("permissionId") Long permissionId);

} 